<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import type { DepartmentSubmitRate } from '@/types/statistics'

const props = defineProps<{
  data?: DepartmentSubmitRate[]
}>()

const chartRef = ref<HTMLElement>()
let chart: echarts.ECharts

onMounted(() => {
  if (chartRef.value) {
    chart = echarts.init(chartRef.value)
    updateChart()
  }
})

watch(() => props.data, updateChart, { deep: true })

function updateChart() {
  if (!chart || !props.data) return
  
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      boundaryGap: [0, 0.01]
    },
    yAxis: {
      type: 'category',
      data: props.data.map(item => item.departmentName)
    },
    series: [
      {
        name: '提交率',
        type: 'bar',
        data: props.data.map(item => (item.submitRate * 100).toFixed(2))
      }
    ]
  }
  
  chart.setOption(option)
}
</script>

<template>
  <div ref="chartRef" style="width: 100%; height: 300px;"></div>
</template> 